package com.game.model;

import com.game.global.Const;
import com.game.global.CrashType;
import com.game.interfaces.CollisionAble;
import com.game.interfaces.LifeCycle;
import com.game.main.Executor;
import com.game.main.GameContext;
import com.game.util.ImageUtil;

import java.awt.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: tjc
 * @description
 * @Date Created in 22:45 2021-08-03
 */
public class BirthPlace extends Module implements CollisionAble, LifeCycle {
    public static final int S_FREE = 0;
    public static final int S_BUILDING = 1;
    private AtomicInteger status = new AtomicInteger(S_FREE); // free, building
    private AtomicInteger pictNo = new AtomicInteger(0);
    AtomicBoolean alive = new AtomicBoolean(false);

    private BlockingQueue<TankConfig> queue = new ArrayBlockingQueue<>(10);

    public BirthPlace() {
    }

    public BirthPlace(int x, int y) {
        localX = x;
        localY = y;
        SIZE_X = 35;
        SIZE_Y = 35;
        direction = Const.DIRECTION_UP;
    }

    @Override
    public void init() {
        alive.set(true);
        Executor.async(this::buildTankTask, this::ShineTask);
    }

    @Override
    public void destroy() {
        alive.set(false);
    }

    @Override
    public boolean alive() {
        return alive.get();
    }

    private void buildTankTask() {
        try {
            while (alive()) {
                if (isBlocking()) {
                    Thread.sleep(2000);
                    continue;
                }
                TankConfig config = queue.take();
                Thread.sleep(2000);
                status.set(S_BUILDING);
                Thread.sleep(1000);
                Tank tank = buildTank(config);
                tank.init();
                tank.start();
                GameContext.getContext().tankList.add(tank);
                // 发送给control
                status.set(S_FREE);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void ShineTask() {
        while (alive()) {
            pictNo.set(pictNo.get() % 3);
            pictNo.getAndIncrement();
            try {
                Thread.sleep(150);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private boolean isBlocking() {
        for (Tank t : GameContext.getContext().tankList) {
            if (this.getRec().intersects(t.getRec())) {
                return true;
            }
        }
        return false;
    }

    public void create(TankConfig config) {
        queue.add(config);
    }

    public Tank buildTank(TankConfig config) {
        config.setX(this.localX);
        config.setY(this.localY);
        return SimpleTankFactory.createTank(config);
    }

    public void draw(Graphics g) {
        if (status.get() == S_BUILDING) {
            int width = ImageUtil.appearImage.getWidth(null);
            int height = ImageUtil.appearImage.getHeight(null);
            g.drawImage(ImageUtil.appearImage, localX, localY, localX + SIZE_X, localY + SIZE_Y, width / 3 * (pictNo.get()), 0, width / 3 * (pictNo.get() + 1), height, null);
        }
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(localX, localY, SIZE_X, SIZE_Y);
    }

    @Override
    public int getCollisionType() {
        return CrashType.BIRTHPLACE;
    }

    @Override
    public int getRight() {
        return localX + SIZE_X;
    }

    @Override
    public int getBottom() {
        return localY + SIZE_Y;
    }
}
